﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Controls
{
	public class DateSearchItem : SearchItem
	{
		private Telerik.Windows.Controls.RadDatePicker _StartPicker;
		protected Telerik.Windows.Controls.RadDatePicker StartPicker
		{
			get
			{
				if (this._StartPicker == null)
				{
					this._StartPicker = new Telerik.Windows.Controls.RadDatePicker();
					this._StartPicker.Width = 120;
					this._StartPicker.DisplayFormat = Telerik.Windows.Controls.DateTimePickerFormat.Long;
					this._StartPicker.DateTimeWatermarkContent = "";
				}
				return this._StartPicker;
			}
		}

		private Telerik.Windows.Controls.RadDatePicker _EndPicker;
		protected Telerik.Windows.Controls.RadDatePicker EndPicker
		{
			get
			{
				if (this._EndPicker == null)
				{
					this._EndPicker = new Telerik.Windows.Controls.RadDatePicker();
					this._EndPicker.Width = 120;
					this._EndPicker.DisplayFormat = Telerik.Windows.Controls.DateTimePickerFormat.Long;
					this._EndPicker.DateTimeWatermarkContent = "";
				}
				return this._EndPicker;
			}
		}

		public System.DateTime? StartTime
		{
			get
			{
				return this._StartPicker.SelectedDate;
			}
			set
			{
				this._StartPicker.SelectedDate = value;
			}
		}

		public System.DateTime? EndTime
		{
			get
			{
				return this._EndPicker.SelectedDate;
			}
			set
			{
				this._EndPicker.SelectedDate = value;
			}
		}


		private System.Windows.Controls.Grid _SimpleSearcherInput;
		public override System.Windows.FrameworkElement SimpleSearcherInput
		{
			get
			{
				if (this._SimpleSearcherInput == null)
				{
					this._SimpleSearcherInput = new System.Windows.Controls.Grid();
					this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
					this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
					this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

					this._SimpleSearcherInput.Children.Add(this.StartPicker);

					System.Windows.Controls.TextBlock text = new System.Windows.Controls.TextBlock { Text = "至",VerticalAlignment=System.Windows.VerticalAlignment.Center };
					this._SimpleSearcherInput.Children.Add(text);
					System.Windows.Controls.Grid.SetColumn(text, 1);

					this._SimpleSearcherInput.Children.Add(this.EndPicker);
					System.Windows.Controls.Grid.SetColumn(this.EndPicker, 2);
				}
				return this._SimpleSearcherInput;
			}
		}

		/*
	{ Text: "包含", Value: "{0}.Contains({1})" },
	{ Text: "不包含", Value: "!{0}.Contains({1})" },
	{ Text: "等于", Value: "{0}=={1}" },
	{ Text: "始于", Value: "{0}.StartsWith({1})" },
	{ Text: "非始于", Value: "!{0}.StartsWith({1})" },
	{ Text: "终于", Value: "{0}.EndsWith({1})" },
	{ Text: "非终于", Value: "!{0}.EndsWith({1})" }
		 */
		private Operator[] _OperatorOptions;
		public override Operator[] Operators
		{
			get
			{
				if (this._OperatorOptions == null)
					this._OperatorOptions = new Operator[]
					{
						new Operator(">","{0}>{1}"),
						new Operator("≥","{0}>={1}"),
						new Operator("<","{0}<{1}"),
						new Operator("≤","{0}<={1}"),
						new Operator("=","{0}={1}"),
						new Operator("≠","{0}!={1}")
					};
				return this._OperatorOptions;
			}
		}

		public override System.Windows.FrameworkElement AdvanceSearcherInput
		{
			get
			{
				Telerik.Windows.Controls.RadDatePicker picker = new Telerik.Windows.Controls.RadDatePicker();
				//picker.DateInput.DateFormat = "yyyy年M月d日";
				return picker;
			}
		}

		public override Searcher.WhereCommand GetCommand(string field, int index)
		{
			string command = null;
			System.Collections.ArrayList parameters = new System.Collections.ArrayList();
			if (this.StartPicker.SelectedDate != null)
			{
				command = string.Format("{0}>=@{1}", field, index + parameters.Count);
				parameters.Add(this.StartPicker.SelectedDate);
			}
			if (this.EndPicker.SelectedDate != null)
			{
				if (command != null)
					command += " and ";
				command += string.Format("{0}<@{1}", field, index + parameters.Count);
				parameters.Add(this.EndPicker.SelectedDate.Value.AddDays(1));
			}
			if (parameters.Count == 0)
				return null;
			return new Sunny.Controls.Searcher.WhereCommand { Text = command, Parameters = parameters.ToArray() };
		}

		public override void Empty()
		{
			this.StartPicker.SelectedDate = null;
			this.EndPicker.SelectedDate = null;
		}
	}
}